#coding=utf-8
from uliweb.orm import *
from uliweb.utils.common import get_var

def get_modified_user():
    from uliweb import request
    
    return request.user.id

class QuestionHis(Model):
    __verbose_name__ = u'传票历史表'
    
    weekreport = Field(int, verbose_name='周报')
    old_id = Field(int, verbose_name='原ID')
    requirement = Reference('requirement', verbose_name='关联准开发需求')
    task = Reference('tasks_develop', verbose_name='关联任务')
    category = Field(CHAR, max_length=1, verbose_name='分类', choices=get_var('TICKET/CATETORY'))
    content = Field(TEXT, verbose_name='问题描述')
    progress = Field(str, max_length=200, verbose_name='进度情况')
    solve_desc = Field(TEXT, verbose_name='解决措施')
    owner = Reference('user', verbose_name='所有人', collection_name='owner_questionhis')
    submitter = Field(str, verbose_name='提出人', max_length=60)
    submit_date = Field(datetime.date, verbose_name='提出日期')
    assigned = Field(str, verbose_name='责任人或单位', max_length=80)
    plan_finish_date = Field(datetime.date, verbose_name='计划关闭日期')
    finish_date = Field(datetime.date, verbose_name='实际关闭日期')
    status = Field(CHAR, max_length=1, verbose_name='状态', choices=get_var('TICKET/STATUS_Q'), default='1')
    priority = Field(CHAR, max_length=1, verbose_name='优先级', choices=get_var('TICKET/PRIORITY'), default='3')
    modified_date = Field(datetime.datetime, verbose_name='修改时间')
    modified_user = Reference('user', verbose_name='修改人')
    systems = ManyToMany('system', verbose_name="关联系统")
    title = Field(str, max_length=200, verbose_name='标题')
    kfzx = Reference('kfzx', verbose_name='提出开发中心')
    tx = Reference('tx', verbose_name='提出条线')
    owner_sys = Reference('system', verbose_name='所属系统', collection_name='owner_sys_questions_his')
    
    class Table:
        fields = [
            {'name':'category', 'width':40, 'sortable':True},
            {'name':'priority', 'width':50, 'sortable':True},
            {'name':'status', 'width':50, 'sortable':True},
#            {'name':'progress', 'verbose_name':'进度情况', 'width':60},
            {'name':'title', 'width':250},
#            {'name':'solve_desc', 'width':200},
            {'name':'submitter'},
            {'name':'submit_date'},
            {'name':'assigned'},
            {'name':'plan_finish_date'},
            {'name':'finish_date'},
        ]
    
class RiskHis(Model):
    __verbose_name__ = u'风险历史表'
    
    weekreport = Field(int, verbose_name='周报')
    old_id = Field(int, verbose_name='原ID')
    requirement = Reference('requirement', verbose_name='关联准开发需求')
    task = Reference('tasks_develop', verbose_name='关联任务')
    category = Field(CHAR, max_length=1, verbose_name='分类', choices=get_var('TICKET/CATETORY'))
    content = Field(TEXT, verbose_name='风险描述')
    affect = Field(TEXT, verbose_name='影响结果')
    solve_desc = Field(TEXT, verbose_name='缓解计划')
    resolusion = Field(TEXT, verbose_name='应用方案')
    probability = Field(DECIMAL, verbose_name='概率', precision=2, scale=1, choices=get_var('TICKET/PROBABILITY'))
    owner = Reference('user', verbose_name='所有人', collection_name='owner_riskhis')
    submitter = Field(str, verbose_name='提出人', max_length=60)
    submit_date = Field(datetime.date, verbose_name='提出日期')
    assigned = Field(str, verbose_name='责任人或单位', max_length=80)
    status = Field(CHAR, max_length=1, verbose_name='状态', choices=get_var('TICKET/STATUS_R'), default='1')
    serious = Field(DECIMAL, verbose_name='严重性', choices=get_var('TICKET/SERIOUS'), required=True)
    modified_date = Field(datetime.datetime, verbose_name='修改时间')
    modified_user = Reference('user', verbose_name='修改人')
    systems = ManyToMany('system', verbose_name="关联系统")
    title = Field(str, max_length=200, verbose_name='标题')
    kfzx = Reference('kfzx', verbose_name='提出开发中心')
    tx = Reference('tx', verbose_name='提出条线')
    owner_sys = Reference('system', verbose_name='所属系统', collection_name='owner_sys_risks_his')
    
    class Table:
        fields = [
            {'name':'category', 'width':40, 'sortable':True},
            {'name':'status', 'width':40, 'sortable':True},
            {'name':'degree', 'verbose_name':'暴光度', 'width':50},
            {'name':'title', 'width':200},
#            {'name':'affect', 'width':200},
            {'name':'submitter'},
            {'name':'submit_date'},
            {'name':'assigned'},
#            {'name':'solve_desc', 'width':200},
#            {'name':'resolusion', 'width':200},
            {'name':'probability', 'width':40, 'sortable':True},
            {'name':'serious', 'width':50, 'sortable':True},
        ]
    
class PlanHis(Model):    
    __verbose_name__ = u'计划历史'

    weekreport = Field(int, verbose_name='周报')
    old_id = Field(int, verbose_name='原ID')
    flag = Field(CHAR, verbose_name='计划类型', choices=get_var('PARA/WEEKREPORT_PLAN_TYPE'))
    task = Reference('tasks_develop', verbose_name="开发任务", collection_name='tasks_planhis', index=True)
    parent_task = Reference('tasks_develop', verbose_name="主任务", collection_name='parent_tasks_planhis', index=True)
    title = Field(str, verbose_name="名称", max_length=40, required=True)
    type = Field(CHAR, max_length=1, verbose_name="计划类型", choices=get_var('PARA/PLAN_TYPE'), required=True)
    begin_date = Field(datetime.date, verbose_name="计划开始时间")
    end_date = Field(datetime.date, verbose_name="计划结束时间")
    test_type = Field(bool, verbose_name="测试相关")
    memo = Field(TEXT, verbose_name="备注")
    idn = Field(str, verbose_name="标识信息", max_length=4)
    main_sys = Reference('system', verbose_name="计划系统", collection_name='system_planhis')
    systems = ManyToMany('system', verbose_name="执行系统", collection_name='systems_planhis')
    is_public = Field(bool, verbose_name="是否公共")
    is_reserved = Field(bool, verbose_name="是否保留")
    stage = Field(CHAR, verbose_name='所处阶段',max_length=2, choices=get_var('PARA/TASK_STAGE'), default='00')
    weight = Field(float, verbose_name="权重", default=0.1)
    responsible = Field(str, max_length=200, verbose_name="责任人")
    modified_date = Field(datetime.datetime, verbose_name='修改时间')
    modified_user = Reference('user', verbose_name='修改人')
    
    class NextTable:
        fields = [
            'type', 'title', 'begin_date', 'end_date', 'systems', 'responsible'
        ]

class PlanExecuteHis(Model):
    __verbose_name__ = u'计划执行历史表'
    
    weekreport = Field(int, verbose_name='周报')
    old_id = Field(int, verbose_name='原ID')
    task = Reference('tasks_develop', verbose_name="开发任务", collection_name='tasks_planexecutehis')
    plan = Reference('plan', verbose_name="计划", collection_name="plan_executehis")
    type = Field(CHAR, max_length=1, verbose_name="计划类型", choices=get_var('PARA/PLAN_TYPE'), required=True)
    system = Reference('system', verbose_name="执行系统")
    finish_begin_date = Field(datetime.date, verbose_name="实际开始时间")
    finish_end_date = Field(datetime.date, verbose_name="实际结束时间")
    status = Field(CHAR, verbose_name="执行状态", max_length=1, choices=get_var('PARA/PLAN_STATUS'))
    memo = Field(TEXT, verbose_name="备注")
    submission = Field(FILE, verbose_name='提交件')
    delay_classes = Field(CHAR, max_length=2, verbose_name='延迟原因分类', choices=get_var('PARA/TASK_DELAY_CLASSES'))
    delay_reason = Field(TEXT, verbose_name='延迟原因说明')
    modified_date = Field(datetime.datetime, verbose_name='修改时间')
    modified_user = Reference('user', verbose_name='修改人')
    sync_date = Field(datetime.date, verbose_name="上次更新日期")
    desc = Field(TEXT, verbose_name='完成情况说明')
    executer = Field(str, verbose_name='执行人', max_length=80)
    finish = Field(float, verbose_name='实际完成进度', default=0.0)
    
class WeekReport(Model):
    __verbose_name__ = u'周报'
    
    begin_date = Field(datetime.date, verbose_name='开始时间')
    end_date = Field(datetime.date, verbose_name='结束时间')
    
    requirement = Reference('requirement', verbose_name='准开发需求', required=True)
    kfzx = Reference('kfzx', verbose_name='开发中心')
    #from requirement
    title = Field(str, verbose_name='事项名称', max_length=250)
    req_id = Field(str, verbose_name='事项编号', max_length=12)
    care_level = Field(CHAR, verbose_name='关注级别', default='1', choices=get_var('PARA/CARE_LEVEL'))
    mode = Field(CHAR, verbose_name='实施方式', max_length=2, choices=get_var('PARA/REQ_MODE'))
    tx = Reference('tx', verbose_name='技术部主管条线', index=True, collection_name='tx_weekreports')
    tx_manager = Field(str, max_length=80, verbose_name='条线责任人')
    submitter = Field(str, max_length=80, verbose_name='提出部门')
    contactor = Field(str, max_length=80, verbose_name='业务联系人')
    exe_manager = Field(str, max_length=80, verbose_name='实施负责人')
    main_sys = Reference('system', verbose_name='主系统', collection_name='main_sys_weekreports')
    systems = ManyToMany('system', verbose_name='执行系统', collection_name='sys_weekreports')

    #other information
    description = Field(TEXT, verbose_name='事项进展简单要描述')
    pre_execute = Field(float, verbose_name='预计执行')
    current_status = Field(CHAR, max_length=1, verbose_name='实际进展', choices=get_var('PARA/WEEKREPORT_STATUS'))
    lastest_version_date = Field(datetime.date, verbose_name='最近投产日期')
    versions = Field(int, verbose_name='拟协同投产版本数')
    req_change_date = Field(datetime.date, verbose_name='需求变更日期')
    req_change_content = Field(TEXT, verbose_name='需求变更内容')
    req_change_according = Field(TEXT, verbose_name='需求变更依据')
    design_change_date = Field(datetime.date, verbose_name='方案变更日期')
    design_change_content = Field(TEXT, verbose_name='方案变更内容')
    design_change_according = Field(TEXT, verbose_name='方案变更依据')
    plan_resource_center = Field(float, verbose_name='计划投入中心资源')
    input_center_per = Field(float, verbose_name='中心已投入比例')
    plan_resource_company = Field(float, verbose_name='计划投入公司资源')
    input_company_per = Field(float, verbose_name='公司已投入比例')
    assignments = ManyToMany('assignment', verbose_name='任务书信息')
    
    #test
    func_passed_per = Field(float, verbose_name='功能测试通过率')
    unfunc_passed_per = Field(float, verbose_name='非功能测试通过率')
    maintain_passed_per = Field(float, verbose_name='运维测试通过率')
    
    modified_date = Field(datetime.datetime, verbose_name='修改时间')
    modified_user = Reference('user', verbose_name='修改人', auto=True, auto_add=True)
    
    class Table:
        fields = [
            {'name':'id', 'verbose_name':'编号', 'width':35, 'sortable':True},
            {'name':'begin_date', 'width':75},
            {'name':'end_date', 'width':75},
            {'name':'title', 'width':300},
            {'name':'care_level', 'width':70, 'sortable':True},
            {'name':'mode', 'width':120, 'sortable':True},
            {'name':'kfzx', 'width':60, 'sortable':True},
            {'name':'tx', 'sortable':True},
            {'name':'tx_manager'},
            {'name':'submitter'},
            {'name':'contactor'},
            {'name':'exe_manager'},
            {'name':'main_sys'},
#            {'name':'description', 'width':200},
            {'name':'pre_execute', 'align':'right'},
            {'name':'current_status', 'sortable':True},
            {'name':'lastest_version_date', 'width':75, 'sortable':True},
            {'name':'versions', 'align':'center', 'sortable':True},
            {'name':'plan_resource_center', 'align':'right'},
            {'name':'input_center_per', 'align':'right'},
            {'name':'plan_resource_company', 'align':'right'},
            {'name':'input_company_per', 'align':'right'},
        ]
    
    class EditDescForm:
        fields = ['description']
    